package com.scau.controller;

import com.scau.dto.CBECReturn;
import com.scau.dto.CashReFrom;
import com.scau.dto.Order;
import com.scau.dto.PayForm;
import com.scau.exception.GoodsStockException;
import com.scau.handleException.ResultCode;
import com.scau.pojo.EWallet;
import com.scau.pojo.PaymentFlow;
import com.scau.service.IEWalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Controller
@CrossOrigin
@RequestMapping("")
public class EWalletSignController {
    @Autowired
    IEWalletService eWalletService;

    @RequestMapping("/wallet/toSignPage")
    public String toSignPage(){
        return "EWalletSign";
    }


    @RequestMapping(value = "/wallet/update",method = RequestMethod.PUT,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn WalletUpdate(@RequestBody EWallet eWallet){

        if(eWalletService.updateWallet(eWallet) == 1){
            return CBECReturn.build(ResultCode.SUCCESS);
        } else{
            return CBECReturn.build(ResultCode.Failed);
        }

    }
//    public List<OrderItem> doList(String str){
//        JSONArray jsonArray= JSONArray.fromObject(str);
//        List<OrderItem> list=(List)JSONArray.toCollection(jsonArray,T.class);
//        return list;
//    }


    @RequestMapping(value = "/wallet/pay", method = RequestMethod.POST,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn Pay(@RequestBody PayForm payForm) throws GoodsStockException {
        System.out.println(payForm);
        String msg = eWalletService.pay(payForm);
        System.out.println(msg);
        return CBECReturn.build(ResultCode.SUCCESS);
    }

    @RequestMapping(value="/wallet/list/{username}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn listOrderItem(@PathVariable String username){

        List<Order> orders = eWalletService.ListByBuyer(username);
        System.out.println("返回数据");
        return CBECReturn.build(ResultCode.SUCCESS,orders);
//        PayForm payForm = new PayForm();
//        payForm.setOrders(orders);
//        EWallet wallet = new EWallet();
//        wallet.setUsername("113");
//        wallet.setPassword("123");
//        payForm.setWallet(wallet);
//        ObjectMapper objectMapper = new ObjectMapper();
//        String json = objectMapper.writeValueAsString(payForm);
//        System.out.println(json);

    }

    //根据username 获取 ewallet
    @RequestMapping(value="/wallet/getWallet/{username}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn getWallet(@PathVariable String username){
        EWallet wallet = eWalletService.getWalletByUsername(username);
        return CBECReturn.build(ResultCode.SUCCESS,wallet);
    }

    //品牌商查看流水
    @RequestMapping(value="/wallet/getPaymentFlowByFromId/{username}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn ListPaymentFlowByFromId(@PathVariable String username){
        List<PaymentFlow> paymentFlows = eWalletService.getPaymentFlowByFromId(username);
        if(paymentFlows != null)
             return CBECReturn.build(ResultCode.SUCCESS,paymentFlows);
        else
            return CBECReturn.build(ResultCode.Failed);
    }

    //品牌商查看流水
    @RequestMapping(value="/wallet/getPaymentFlowByToId/{username}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn ListPaymentFlowByToId(@PathVariable String username){
        List<PaymentFlow> paymentFlows = eWalletService.getPaymentFlowByToId(username);
        if(paymentFlows != null)
            return CBECReturn.build(ResultCode.SUCCESS,paymentFlows);
        else
            return CBECReturn.build(ResultCode.Failed);
    }

    //提现
    @RequestMapping(value="/wallet/CashOut",method = RequestMethod.POST,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn CashOut(@RequestBody CashReFrom cashReFrom){
        System.out.println(cashReFrom);
        int result = eWalletService.CashOutOrRechange(cashReFrom,1);
        if (result == 1)
            return CBECReturn.build(ResultCode.SUCCESS);
        else
            return CBECReturn.build(ResultCode.Failed);
//        return CBECReturn.build(ResultCode.SUCCESS);

    }

    //充值
    @RequestMapping(value="/wallet/recharge",method = RequestMethod.POST,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn recharge(@RequestBody CashReFrom cashReFrom){

        int result = eWalletService.CashOutOrRechange(cashReFrom,2);
        if (result == 1)
            return CBECReturn.build(ResultCode.SUCCESS);
        else
            return CBECReturn.build(ResultCode.Failed);
    }


}
